package rbacControllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"lazyops_go/models"
)

type MenuController struct {
	beego.Controller
}

func (c *MenuController)  IndexMenu() {
	menu_list := models.QueryMenuAll()
	menu_level_list := models.QueryMenuLevelOne()
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["menu_list"] = menu_list
	c.Data["menu_level_list"] = menu_level_list
	c.Data["label"] = "菜单管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/menujs.html"
	c.TplName = "rbac/menu.html"

}

//添加
func (c *MenuController)  CreateMenu() {
	is_menu,_:= c.GetBool("is_menu",false)
	p_menu,_:= c.GetInt("p_menu",0)
	menu_title := c.GetString("menu_title")
	menu_icon := c.GetString("menu_icon")
	menu_url := c.GetString("menu_url")
	menu_weight,_:= c.GetInt("menu_weight",0)
	menu_status,_ := c.GetBool("menu_status",false)

	menu_obj := models.Menu{}
	menu_obj.IsMenu = is_menu
	menu_obj.MenuTitle = menu_title
	menu_obj.PMenu = p_menu
	menu_obj.MenuIcon = menu_icon
	menu_obj.Status = menu_status
	menu_obj.MenuUrl = menu_url
	menu_obj.MenuWeight = menu_weight

	models.AddMenu(menu_obj)
	c.Data["json"] = "add Success"
	c.ServeJSON()
}

func (c *MenuController)  GetMenuById() {
	m_id,_ := c.GetInt("m_id",0)
	menu := models.GetMenuId(m_id)
	c.Data["json"] = menu
	c.ServeJSON()
}

func (c *MenuController)  EditMenu() {
	m_id,_ := c.GetInt("m_id",0)
	is_menu,_:= c.GetBool("is_menu",false)
	p_menu,_:= c.GetInt("p_menu",0)
	menu_title := c.GetString("menu_title")
	menu_icon := c.GetString("menu_icon")
	menu_url := c.GetString("menu_url")
	menu_weight,_:= c.GetInt("menu_weight",0)
	menu_status,_ := c.GetBool("menu_status",false)

	menu := models.Menu{}
	menu.MenuTitle = menu_title
	menu.MenuUrl = menu_url
	menu.MenuWeight = menu_weight
	menu.MenuIcon = menu_icon
	menu.IsMenu = is_menu
	menu.Status = menu_status
	menu.PMenu = p_menu
	fmt.Println(m_id,menu)
	models.UpdateMenu(m_id,menu)

	c.Data["json"] = "修改成功"
	c.ServeJSON()
}


//删除角色
func (c *MenuController) DelMenu() {
	m_id,_:= c.GetInt("m_id", 0)
	models.DeleteMenu(m_id)
	c.Data["json"] = "删除成功"
	c.ServeJSON()
}

//搜索
func (c *MenuController) SearchMenu() {
	key := c.Ctx.Input.Param(":key")
	menu_list := models.QueryMenuSearch(key)
	menu_level_list := models.QueryMenuLevelOne()
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["key"] = key
	c.Data["menu_list"] = menu_list
	c.Data["menu_level_list"] = menu_level_list
	c.Data["label"] = "菜单管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "rbac/menujs.html"
	c.TplName = "rbac/menu.html"
}

